from itsdangerous import TimedJSONWebSignatureSerializer
from itsdangerous import JSONWebSignatureSerializer
from django.conf import settings


# 对openid进行加密
def generate_sign_openid(openid):
    # 创建TimedJSONWebSignatureSerializer 对象
    serializer = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
    # 加密数据
    sign_openid = serializer.dumps({'openid': openid})
    # 返回数据
    return sign_openid.decode()

# 对openid进行解密
def decode_sign_openid(sign_openid):
    # 创建TimedJSAONWebSignatureSerializer对象
    serializer = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=300)
    # 解密数据
    try:
        dict_data = serializer.loads(sign_openid)
    except Exception as e:
        return None

    openid = dict_data.get('openid')
    # 返回数据
    return openid
